package com.yingdong.logic.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.yingdong.db.Resource;
import com.yingdong.logic.domain.GameUser;
import com.yingdong.message.ResourceMessage;

public class ResourceController extends GameController {
	private Map<Integer, Resource> resourceMap = new ConcurrentHashMap<>();

	public Map<Integer, Resource> getResourceMap() {
		return resourceMap;
	}

	public void setResourceMap(Map<Integer, Resource> resourceMap) {
		this.resourceMap = resourceMap;
	}

	public ResourceController(GameUser user) {
		super(user);

	}

	@Override
	public void init() {
		List<Resource> list = Resource.getInstance().selectObjectsByCondition("userId", getUserId());
		for (Resource resource : list) {
			resourceMap.put(resource.getItemId(), resource);
		}
	}

	@Override
	public void firstEnter() {
		new ResourceMessage(resourceMap).sendToClient(getSessionId());
	}

	@Override
	public void saveDB() {
		Resource.getInstance().saveBatch(resourceMap.values());
	}

	public void addResource(int itemId, int count) {

	}
}
